package p3.game;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import p3.structures.Tree;

public class BoardPanel extends JPanel{

	private static final long serialVersionUID = -8581009676061191163L;
	
	protected GameBoard game;
	protected JPanel[][] panelHolder;
	private JButton b_next;

	private Tree tree;
	
	int prev_defender_x, prev_defender_y;
	int prev_alien_x, prev_alien_y;
	int prev_rocket_x, prev_rocket_y;

	private JTabbedPane parent = null;
	
	public BoardPanel(){
		game = new GameBoard();
		game.randomInit();
		
		tree = null;
		setLayout(new BorderLayout());
		add(getButton(),BorderLayout.SOUTH);
		add(getBoard(),BorderLayout.CENTER);
				
	}
	
	public BoardPanel(GameBoard game){
		this.game = game;
		
		tree = null;
		
		setLayout(new BorderLayout());
		add(getButton(),BorderLayout.SOUTH);
		add(getBoard(),BorderLayout.CENTER);	
				
	}
	
	public BoardPanel(JTabbedPane frm) {
		parent = frm;
		
		game = new GameBoard();
		game.randomInit();
		
		tree = null;
		
		setLayout(new BorderLayout());
		add(getButton(),BorderLayout.SOUTH);
		add(getBoard(),BorderLayout.CENTER);
	}

	public JPanel getBoard(){
		JPanel board = new JPanel();
		board.setLayout(new GridLayout(game.SIZE, game.SIZE));
		panelHolder = new JPanel[game.SIZE][game.SIZE]; 
		for(int m = 0; m < game.SIZE; m++) {
		   for(int n = 0; n < game.SIZE; n++) {
		      panelHolder[m][n] = new JPanel();
		      panelHolder[m][n].setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200)));
		      board.add(panelHolder[m][n]);
		   }
		}
		return board;
	}
	
	public JPanel getButton(){
		JPanel ret = new JPanel();
		ret.setLayout(new BorderLayout());
		
		b_next = new JButton("Step");
		b_next.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				game.step();
				refresh();
			}
		});
		ret.add(b_next, BorderLayout.SOUTH);
		
		JButton b_run = new JButton("Ejecuta");
		b_run.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (tree != null) execute();
			}
		});
		ret.add(b_run, BorderLayout.NORTH);
		
		return ret;
	}
	
	public void setValues(){
		
		if (prev_alien_x != game.alien.x || prev_alien_y != game.alien.y){
			panelHolder[prev_defender_y][prev_defender_x].removeAll();
			panelHolder[prev_alien_y][prev_alien_x].removeAll();
			if (prev_rocket_x >= 0 && prev_rocket_x < game.SIZE && prev_rocket_y >= 0 && prev_rocket_y < game.SIZE){
				panelHolder[prev_rocket_y][prev_rocket_x].removeAll();
			}
		}
		
		panelHolder[game.defender.y][game.defender.x].add(new JLabel("D"));
		panelHolder[game.alien.y][game.alien.x].add(new JLabel("A"));
		if (game.rocketInBoard) 
			if (prev_rocket_x >= 0 && prev_rocket_x < game.SIZE && prev_rocket_y >= 0 && prev_rocket_y < game.SIZE)
				panelHolder[game.rocket.y][game.rocket.x].add(new JLabel("R"));

		prev_defender_x = game.defender.x;
		prev_defender_y = game.defender.y;
		prev_alien_x= game.alien.x;
		prev_alien_y= game.alien.y;
		prev_rocket_x= game.rocket.x;
		prev_rocket_y= game.rocket.y;
		
	}
	
	public void refresh(){
		setValues();
		
		if (parent != null){
			parent.revalidate();
			parent.repaint();
		}
		
		revalidate();
		repaint();
				
	}
	
	public void refresh(int ms){
		refresh();
		if (ms <= 0) return;
		try {
			Thread.sleep(ms);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public final int refreshTime = 200;
	
	public void execute(){
		b_next.setEnabled(false);
		
		int prev_time;
		game.resetTime();
		while (!game.isFinished()){
			prev_time = game.getTime();
			game.execute(tree, this);
			if (prev_time == game.getTime()){
				game.step(this);
			}
		}
		refresh();
		b_next.setEnabled(true);
	}
	
	public static void main(String[] args){
		JFrame frame = new JFrame();

		frame.setSize(565, 600);
		BoardPanel bp = new BoardPanel(); 
		frame.add(bp);		
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Tree tree = new Tree();
		tree.generateChilds(0, 4);
		bp.setTree(tree);
		System.out.println(tree);
		bp.execute();
	}

	public void setTree(Tree tree) { this.tree = tree; }
}
